package com.escapeicon.bigevent.controller;

import com.escapeicon.bigevent.pojo.dto.PageBean;
import com.escapeicon.bigevent.pojo.entity.Article;
import com.escapeicon.bigevent.pojo.vo.ResultVO;
import com.escapeicon.bigevent.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    public ResultVO add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return ResultVO.success();
    }

    @GetMapping
    public ResultVO<PageBean<Article>> getArticleForPage(
        Integer pageNum,
        Integer pageSize,
        @RequestParam(required = false) Integer categoryId,
        @RequestParam(required = false) String state
    ) {
        PageBean<Article> articlePageBean = articleService.getArticleForPage(pageNum,pageSize,categoryId,state);
        return ResultVO.success(articlePageBean);
    }

    @GetMapping("/detail")
    public ResultVO<Article> getDetail(Integer id){
        Article article = articleService.getDetail(id);
        return ResultVO.success(article);
    }

    @PutMapping
    public ResultVO update(@RequestBody @Validated(Article.Update.class) Article article) {
        articleService.update(article);
        return ResultVO.success();
    }

    @DeleteMapping
    public ResultVO delete(Integer id){
        articleService.delete(id);
        return ResultVO.success();
    }
}
